﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;

//百度搜索引擎不知道为什么不能被C#获取到
//所以我是用的是bing，其他都是按照要求写的

namespace WinFormsApp9
{
    public partial class Form1 : Form
    {

        private delegate void MyDelegate();
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart st1 = new ThreadStart(Search_baidu1);
            ThreadStart st2 = new ThreadStart(Search_baidu2);
            Thread t1 = new Thread(st1);
            Thread t2 = new Thread(st2);
            t1.Start();
            t2.Start();

        }

        private void Search_baidu1()           // baidu
        {
            string url = "https://cn.bing.com/search?form=MOZTSB&pc=MOZI&q=" + textBox1.Text;
            WebClient webClient = new WebClient();
            byte[] recvdata = webClient.DownloadData(url);       // 指定url下载数据到byte数组中
            string response = Encoding.UTF8.GetString(recvdata); // 获取UTF-8类型编码
            string str = noHtml(response);
            string str1 = Regex.Replace(str, @"[A-Za-z]", "");
            this.BeginInvoke(new MyDelegate(() => { textBox2.Text = str1.Substring(0, 200); }));   // 将转换后的数据填入textBox
        }
        private void Search_baidu2()           // baidu
        {
            string url = "https://cn.bing.com/search?form=MOZTSB&pc=MOZI&q=" + textBox1.Text;
            WebClient webClient = new WebClient();
            byte[] recvdata = webClient.DownloadData(url);       // 指定url下载数据到byte数组中
            string response = Encoding.UTF8.GetString(recvdata); // 获取UTF-8类型编码
            string str = noHtml(response);
            string str1 = Regex.Replace(str, @"[A-Za-z]", "");
            this.BeginInvoke(new MyDelegate(() => { textBox3.Text = str1.Substring(0, 200); }));   // 将转换后的数据填入textBox
        }

        public static string noHtml(string str)
        {
            if (str != null)
            {

                str = Regex.Replace(str, @"<script[^>]*>[\s\S]*?</script>", "", RegexOptions.IgnoreCase);//删除脚本
                str = Regex.Replace(str, @"(<style)+[^<>]*>[^\0]*(<\/style>)+", "", RegexOptions.IgnoreCase);//删除样式
                str = Regex.Replace(str, @"<object.*?/object>", "", RegexOptions.IgnoreCase);//删除object
                str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);//删除开始注释
                str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);//删除结尾注释

                str = Regex.Replace(str, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);//删除全部html
                str = Regex.Replace(str, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);//删除换行
                str = Regex.Replace(str, @"  ", "　", RegexOptions.IgnoreCase);//替换空格
            }
            return str;
        }

    }
}